import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const TableComponent 		= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
const $time 				= $Lazy.file(()=>import('@com/utils/time'))
// const Export 				= $Lazy.file(()=>import('#private/common/export'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '客户名称',			name: 'hosp_id', 					type: 'select', loading:true },
			{ label: '实验科室',			name: 'project_parent_id', 			type: 'select', loading:true, idStr:'id' },
			{ label: '区域',			name: 'region_id', 					type: 'select', loading:true },
			{ label: '业务员',			name: 'salesman_id', 				type: 'select', loading:true },
			// { label: '接收标本时间',		names: ['start_at','end_at'], name:'time-null', 	type:'date-range', format:1 },
			{ label: '账单创建日期',		names: ['bill_start_at','bill_end_at'], name:'jz-time', type:'date-range', format:1 },
			{ label: '状态',			name: 'status',						type: 'select', loading:true, idStr:'code' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		
		this.getSelect()
		// 设置时间默认值
		$time.then(f=>{
			const arr = f.duration['1-today']()
			this.resetModel = { bill_start_at: arr[0], bill_end_at: arr[1], 'jz-time':arr }
			this.model = $fn.copy(this.resetModel)
			// this.fetch()
		})
	}
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		
		$cache.then( async f => {
			// 客户名称
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) ) 
			// 实验科室
			$fn.setRow(forms, 'project_parent_id', await f.getCache({ api:'shi_yan_ke_shi' } ) ) 
			// 区域
			$fn.setRow(forms, 'region_id', await f.getCache({ api:'bs_area' } ) ) 
			// 业务员
			$fn.setRow(forms, 'salesman_id', await f.getCache({ api:'salesman' } ) )
			// 状态
			$fn.setRow(forms, 'status', await f.getDicCache({ code:45300 } ) )
			
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'bill-st/constsummary',{isIndex:true, callback: result => this.setState({ result })})
	
	// 表单
	cols = [
		{ title: '序号',				dataIndex: 'sort', 						...$fn.table.num },
		{ title: '客户名称',				dataIndex: 'hosp_name', 				width:250 },
		{ title: '项目名称',				dataIndex: 'kind_name', 				width:250 },
		{ title: '数量',				dataIndex: 'sum_spec_in', 				...$fn.table.num },
		{ title: '冲抵数量',				dataIndex: 'sum_spec_out', 				...$fn.table.num },
		{ title: '实际数量',				dataIndex: 'sum_spec', 					...$fn.table.num },
		{ title: '标准价格',				dataIndex: 'sum_sprice', 				...$fn.table.num },
		{ title: '实际价格',				dataIndex: 'sum_price', 				...$fn.table.num },
		// { title: '初始结算价',			dataIndex: 'sum_initial_price', 			...$fn.table.num },
		{ title: '送检科室',				dataIndex: 'project_parent_name', 		width:150 },
		{ title: '开票单位（NC）',		dataIndex: 'organization_name', 		width:150 },
		{ title: '开票单位编码（NC）',	dataIndex: 'organization_code', 		width:150 },
		{ title: '业务员',				dataIndex: 'salesman_user', 			...$fn.table.name },
		{ title: '业务员编码',			dataIndex: 'salesman_code', 			width:150 },
		{ title: '区域',				dataIndex: 'region_name', 				width:150 },
		{ title: '区域编码',				dataIndex: 'region_num', 				...$fn.table.num }
	]
	
	// 操作按钮
	controls = () => {
		return [
			Controls.exports.call(this, 'bill-st/constsummary', { isSock:true })						// 导出
		]
	}
	
	render(){
		const {result} = this.state
		return (
			<>
				<Page title='项目汇总' controls={this.controls()} >
					{/* 搜索 */}
					{ SearchForm.call(this, { timeStr:'1-today' }) }
					{/* 表格 */}
					<div className='bcf fv ex'>
						{ Table.call(this,{type:1, isIndex:true, isInit: window.isInit,
							onRow:( rows ) => {
								const jzTime = this.searchFormRef.getName('jz-time')
								// const timeNull = this.searchFormRef.getName('time-null')
								const times = {
									start_at: jzTime[0],
									end_at: jzTime[1],
									// bill_start_at: timeNull ? timeNull[0] : '',
									// bill_end_at: timeNull ? timeNull[1] : ''
									}
								this.tableRef.load({ ...rows, ...times })
							},
							suffix: (
								<>
									<div className='ml10 g9'>总数量 <b className='f14 c0'>{result?.total_sum_spec_in}</b></div>
									<div className='ml10 g9'>总冲抵数量 <b className='f14 c0'>{result?.total_sum_spec_out}</b></div>
									<div className='ml10 g9'>总实际数量 <b className='f14 c0'>{result?.total_sum_spec}</b></div>
								</>
							)
						 }) }
					</div>
					{/* 表格 */}
					<div className='bcf fv mt5 ex pt10'>
						<TableComponent onRef={ ref => this.tableRef = ref }/>
					</div>
				</Page>
			</>
		)
	}
}

export default Index